var QQMapWX = require('../../utils/qqmap-wx-jssdk.js');
var util = require('../../utils/util.js'); 
var qqmapsdk;
var remoteUrl = getApp().globalData.remoteurl;
var that;
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    scale: 18,
    markers: [
    ],
    animationData: {},
    hidden: true,
  },
  bindShowAct: function () {
    var petId = that.data.petId;
    wx: wx.request({
      url: remoteUrl + "getPetActivityId",
      data: {
        petId: petId
      },
      method: 'GET',
      success: function (res) {
        if (res.data == "") {
          util.showtoast("您的宠物还没参加活动","none");
        } else {
          wx.navigateTo({
            url: 'actdetail/actdetail?ActivityId=' + res.data,
          })
        }
      }
    })
  },
  bindAddAct: function () {
    var petId = that.data.petId;
    wx: wx.request({
      url: remoteUrl + "getPetActivityId",
      data: {
        petId: petId
      },
      method: 'GET',
      success: function (res) {
        if (that.data.petId == 0) {
          util.showtoast("您还未添加您的宠物","none");
        } else if (res.data != "") {
          util.showtoast("您的宠物已经参与了某项活动", "none");
        } else {
          wx.navigateTo({
            url: 'addact/addact?petId=' + that.data.petId,
          })
        }
      },
      fail: function (res) {
        util.showtoast("网络出错了", "none");
      }
    })
  },
  bindShowBox: function (e) {
    wx.navigateTo({
      url: 'actdetail/actdetail?ActivityId=' + e.currentTarget.dataset.activityid,
    })
  },
  bindExitAct: function () {
    var petId = that.data.petId;
    wx: wx.request({
      url: remoteUrl + "getPetActivityId",
      data: {
        petId: petId
      },
      method: 'GET',
      success: function (res) {
        if (res.data == "") {
          util.showtoast("您的宠物还没参加活动", "none");
        } else {
          wx: wx.request({
            url: remoteUrl + 'exitActivity',
            data: {
              petId: petId
            },
            method: 'POST',
            header: { 'content-type': 'application/x-www-form-urlencoded' },
            success: function (res) {
              util.showtoast("退出该活动成功", "success");
            },
          })
        }
      }
    })
  },
  bindJoinAct: function (e) {
    var petId = that.data.petId;
    wx: wx.request({
      url: remoteUrl + "getPetActivityId",
      data: {
        petId: petId
      },
      method: 'GET',
      success: function (res) {
        if (e.target.dataset.activityid == undefined) {
          util.showtoast("该宠物还没参加活动", "none");
        } else if (res.data != "") {
          util.showtoast("您的宠物已经参加了某项活动", "none");
        } else {
          wx: wx.showModal({
            title: '您确定参与该活动吗?',
            content: e.target.dataset.activityname,
            showCancel: true,
            success: function (res) {
              if (res.confirm == true) {
                wx: wx.request({
                  url: remoteUrl + 'joinActivity',
                  data: {
                    activityId: e.target.dataset.activityid,
                    petId: petId
                  },
                  header: { 'content-type': 'application/x-www-form-urlencoded' },
                  method: 'POST',
                  success: function (res) {
                    util.showtoast("参与活动成功", "success");
                  },
                  fail: function (res) { },
                  complete: function (res) { },
                })
              }
            },
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this;
    var status = wx.getStorageSync("openstatus");
    if (!status) {
      wx: setTimeout(function () {
        util.showtoast("记得去登陆哦", "none");
      }, 2000)
    }
    wx: wx.request({
      url: remoteUrl + "getPetId",
      data: {
        status: status
      },
      method: 'GET',
      success: function (res) {
        that.setData({
          petId: res.data
        })
      },
    })
    wx: wx.getLocation({
      success: function (res) {
        app.globalData.latitude = res.latitude;
        app.globalData.longitude = res.longitude;
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude,
        })
        wx: wx.request({
          url: remoteUrl + "saveaa",
          data: {
            latitude: res.latitude,
            longitude: res.longitude
          },
          method: 'GET',
          success: function (res) {
            for (var i = 0; i < res.data.length; i++) {
              that.setData({
                ['markers[' + i + '].latitude']: res.data[i].latitude,
                ['markers[' + i + '].longitude']: res.data[i].longitude,
                ['markers[' + i + '].id']: i,
                ['markers[' + i + '].iconPath']: "/images/logo2.png",
                ['markers[' + i + '].width']: 45,
                ['markers[' + i + '].height']: 45,
                ['id[' + i + ']']: res.data[i].id
              })
            }
          },
          fail: function (res) {
            util.showtoast("网络出错了", "none");
          },
        })
      }
    })
  },
  mapbind: function (e) {
    this.setData({
      hidden: true
    })
  },
  test: function (e) {
    qqmapsdk = new QQMapWX({
      key: 'M7CBZ-AEJCG-OUJQC-IE75K-YIHBJ-2VF2J'
    });
    // this.setData({
    //   hidden:false
    // })
    var qwe = that.data.id[e.markerId];
    wx: wx.request({
      url: remoteUrl + "getPetAndImage",
      data: {
        id: qwe
      },
      method: 'GET',
      success: function (res) {
        that.setData({
          pet: res.data
        })
      },
    })
    var animation = wx.createAnimation({
      duration: 200
    })
    this.animation = animation;
    animation.translateY(100).step()
    this.setData({
      animationData: animation.export(),
      hidden: false
    })
    setTimeout(function () {
      animation.translateY(0).step()
      that.setData({
        animationData: animation.export()
      })
    }, 10)
    var id = e.markerId;
    var targetLatitude = this.data.markers[id].latitude;
    var targetLongitude = this.data.markers[id].longitude;
    qqmapsdk.calculateDistance({
      to: [{
        latitude: targetLatitude,
        longitude: targetLongitude
      }],
      from: {
        latitude: that.data.latitude,
        longitude: that.data.longitude
      },
      success: function (e) {
        var distance = Math.floor(e.result.elements[0].distance);
        that.setData({
          distance: distance
        })
        console.log("距离" + e.result.elements[0].distance);
      },
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var status = wx.getStorageSync("openstatus");
    wx: wx.request({
      url: remoteUrl + "getPetId",
      data: {
        status: status
      },
      method: 'GET',
      success: function (res) {
        that.setData({
          petId: res.data
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})